﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JUtil;
using JUtil.CodeGenerator;

namespace CodeGenerator
{
    public class DetailViewGenerator
    {
        public DetailViewGenerator(
            DbSetting DbSetting,
            CSharpFileSetting CSharpFileSetting,
            RelatedSql RelatedSql)
        {
            this.DbSetting = DbSetting;
            this.CSharpFileSetting = CSharpFileSetting;
            this.RelatedSql = RelatedSql;

            SelectDalSql = RelatedSql.GetSelectSQLNoOrderBy;

            this.RelatedSql.GetOrderBy = string.Empty;
            this.RelatedSql.GetSelectSQLNoOrderBy= string.Empty;
            this.RelatedSql.GetDeleteSQL= string.Empty;
        }

        string SelectDalSql;
        DbSetting DbSetting;
        CSharpFileSetting CSharpFileSetting;
        RelatedSql RelatedSql;

        private void ExportSqlFile(string filepath)
        {
            CodeGenBase generator = new CodeGenerator.SqlFile(RelatedSql);

            generator.Export(filepath);
        }

        private void ExportDalFile(string filepath)
        {
            RelatedSql RelatedSql = new RelatedSql(this.RelatedSql);

            RelatedSql.GetSelectSQLNoOrderBy = SelectDalSql;

            CodeGenBase generator = new DalFile(
                                        DbSetting,
                                        CSharpFileSetting,
                                        RelatedSql);

            generator.Export(filepath);
        }

        private void ExportBllFile(string filepath)
        {
            CodeGenBase generator = new BllFile(
                                        DbSetting,
                                        CSharpFileSetting,
                                        RelatedSql);

            generator.Export(filepath);
        }

        public void Export(
            string sqlFileDirectory,
            string dalFileDirectory,
            string bllFileDirectory)
        {
            string pathSqlFile = JUtil.Path.File.GetAbsolutePath(sqlFileDirectory, CSharpFileSetting.BLL_CLASS + ".sql");
            ExportSqlFile(pathSqlFile);

            string pathDalFile = JUtil.Path.File.GetAbsolutePath(dalFileDirectory, CSharpFileSetting.DAL_CLASS + ".cs");
            ExportDalFile(pathDalFile);

            string pathBllFile = JUtil.Path.File.GetAbsolutePath(bllFileDirectory, CSharpFileSetting.BLL_CLASS + ".cs");
            ExportBllFile(pathBllFile);
        }
    }
}
